//
//  CollectionViewController.m
//  LOU
//
//  Created by 要帅气的生活 on 15/11/12.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "CollectionViewController.h"
#import "YYCollectionViewCell.h"
#import "DataBaseHandle.h"
#import "LunBoTuViewController.h"
#import "ToolUtil.h"
#import "Size.h"
@interface CollectionViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UIAlertViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *modelArray;

@end

@implementation CollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    [ToolUtil createLine:margin_64 superView:self.view lift:0 width:WindowWidth hight:0.5 color:[UIColor grayColor]];
    [self createTitle:@"我的" midTitle:@"收藏"];
    [self collectionViewSet];
    [self dataHandle];
    
}

- (void)viewWillAppear:(BOOL)animated{
    if (_modelArray.count == 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"亲,还没有任何收藏内容呦,快去添加吧" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles: nil];
        
        [alertView show];
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.selectedIndex = 0;
}
- (void)dataHandle{
    _modelArray = [NSArray new];
    [[DataBaseHandle shareDataBaseHandle] open];
    _modelArray = [[DataBaseHandle shareDataBaseHandle] selectAllCollects];
    [[DataBaseHandle shareDataBaseHandle] close];
    [_collectionView reloadData];
}
- (void)collectionViewSet{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((self.view.frame.size.width - 60 * Width) / 2.0, (self.view.frame.size.width - 60 * Width) / 2.0 + 60 * Height);
    flowLayout.minimumInteritemSpacing = 20 * Width;
    flowLayout.minimumLineSpacing = 20 * Height;
    flowLayout.sectionInset = UIEdgeInsetsMake(20 * Height, 20 * Width, 20 * Height, 20 * Width);
    

    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, self.view.frame.size.height - 65) collectionViewLayout:flowLayout];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, self.view.frame.size.height - (self.navigationController.navigationBar.frame.size.height + 20) - self.tabBarController.tabBar.frame.size.height) collectionViewLayout:flowLayout];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:flowLayout];
    [self.view addSubview:_collectionView];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[YYCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    _collectionView.backgroundColor = [UIColor whiteColor];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    LunBoTuViewController *infoContent = [[LunBoTuViewController alloc] init];
    Enshrine *model = [_modelArray objectAtIndex:indexPath.row];
    NSLog(@"%@",model.picture);
    infoContent.xiangqing.img.image = [UIImage imageNamed:model.picture];
    infoContent.tiao.label1.text  = @"123";
   [self.navigationController pushViewController:infoContent animated:YES];

}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _modelArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    YYCollectionViewCell *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    cell.model = [_modelArray objectAtIndex:indexPath.item];
    
    return cell;
}

@end
